const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const CopyWebpackPlugin=require("copy-webpack-plugin");
const webpack= require("webpack");
const fs=require("fs");

let set_plugins= ()=> {

    let files = fs.readdirSync("./html");
    return files.map((file)=>
        new HtmlWebpackPlugin({
            title: '',
            template:"./html/"+file,
            filename:file,
            chunks:["bootstarp",file.match(/([^\.]+)/)[1]]
        })
    );
};


module.exports = {
    entry:{
        bootstarp:"./src/bootstarp.js",
        index:"./src/index.js",
        popup:"./src/popup.js"
       
    },
    plugins: [
        new CleanWebpackPlugin(['./dist']),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery:'jquery'
        })
    ].concat(
        set_plugins()
    ).concat([
        //new UglifyJSPlugin(),
        new CopyWebpackPlugin([
            {context:'bootstrap-3.3.7-dist',from:"**/*",to:"bootcss"},
            {context:'asset/images',from:"**/*",to:"images"},
            {from:"manifest.json",to:"manifest.json"},
            {from:"./src/popup.js",to:"popup.js"},
            {from:"./src/contentScript.js",to:"contentScript.js"}
            ])
    ]),
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    'file-loader'
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    'file-loader'
                ]
            }
        ]
    }
};